package com.hs.telepass.manager;


import android.content.Context;
import com.hs.telepass.MainApp;
import com.hs.telepass.controller.TaskController;
import com.hs.telepass.dao.TelepassProfilesDatabase;
import com.hs.telepass.model.TelepassProfiles;
import com.hs.telepass.model.json.FetchPubkeyJson;
import com.hs.telepass.model.json.UserInfoJson;
import com.hs.telepass.utils.DeviceInfoUtil;
import com.hs.telepass.utils.JsonUtil;
import com.hs.telepass.utils.LogUtil;
import com.hs.telepass.utils.security.AESEnc;
import com.hs.telepass.utils.security.Base64Utils;
import com.hs.telepass.utils.security.Cert;
import com.hs.telepass.utils.security.RSAEncrypt;

import java.net.URLEncoder;

public class TeleformManager {
    private static TeleformManager mInstance;
    private Context mContext;


    public TeleformManager() {
        mContext = MainApp.getInstance();
    }

    public static TeleformManager getInstance() {
        if (null == mInstance)
            mInstance = new TeleformManager();
        return mInstance;
    }

    public void fetchPublicKey(String runtime_key, String sessionId) throws Exception {
        String uuid = DeviceInfoUtil.getDeviceId(mContext);
        String enc_sessionid = "";

        //生成json
        FetchPubkeyJson json = new FetchPubkeyJson();
        json.setRuntime_key(runtime_key);
        json.setSessionId(sessionId);
        String jsonStr = JsonUtil.toJson(json);
        LogUtil.v("fetch pubkey json : " + jsonStr);

        Cert cert = new Cert(mContext, "public.der");

        String rsaStr = cert.pubKeyEncrypt(jsonStr);
        LogUtil.v("fetch pubkey rsa : " + rsaStr);
        enc_sessionid = rsaStr;

        TaskController.getInstance().fetchPublicKey(mContext, enc_sessionid, uuid);
    }

    public void updateSession(String sessionId, String username, String password, String pubkey, String runtime_key) throws Exception {
        UserInfoJson userInfoJson = new UserInfoJson();
        userInfoJson.setU(username);
        userInfoJson.setP(password);
        String jsonStr = JsonUtil.toJson(userInfoJson);
        LogUtil.v("update json : " + jsonStr);
        int start = pubkey.indexOf("-----BEGIN PUBLIC KEY-----") + "-----BEGIN PUBLIC KEY-----".length();
        int end = pubkey.indexOf("-----END PUBLIC KEY-----");
        pubkey = pubkey.substring(start, end).replaceAll("\\\\n", "");
        LogUtil.v("update pubkey : " + pubkey);
        RSAEncrypt rsaEncrypt = new RSAEncrypt();
        rsaEncrypt.loadPublicKey(pubkey);


        byte[] rsaByte = rsaEncrypt.encrypt(rsaEncrypt.getPublicKey(), jsonStr.getBytes("utf-8"));


        AESEnc aesEnc = new AESEnc(runtime_key.getBytes());
        byte[] aesStr = aesEnc.encrypt(rsaByte);

        String aesBase64 = Base64Utils.encode(aesStr);
        //对特殊字符进行转义
        aesBase64 = URLEncoder.encode(aesBase64);

        TaskController.getInstance().updateSession(mContext, sessionId, aesBase64);
    }

    /**
     * 插入一条teleformProfile数据
     * */
    public void insertTeleformProfile(TelepassProfiles telepassProfiles){
        if(!ProfilesManager.getInstance().hasTotallySameTeleProfile(telepassProfiles))
            new TelepassProfilesDatabase(mContext).addRecord(telepassProfiles);

    }
}
